import numpy as np
import scipy as sp
import sympy as sy
import itertools
#矩阵类函数
Det=np.linalg.det
Transpose=np.ma.transpose
Tr=np.ma.trace

def IntegerDigits(x,n=10,length=None):#进制分解，x为参数，n为基,多截断少补0
    lis=[]
    if length is None:
        while x>0:
            lis.append(x%n)
            x//=n
    else:
        while len(lis)<length:
            lis.append(x%n)
            x//=n
    return reversed(lis)

def FromDigits(lis,n=10):
    return sum([lis[-1-i]*n**i for i in range(len(lis))])
    
def Select(lis,test,n=None):#test返回true则select
    returnlist=[i for i in lis if test(i)==True]
    if n==None:
        return returnlist
    return returnlist[n-1]

def IntegerLength(x):
    i=0
    while x>0:
        x//=10
        i+=1
    return i

def And(*bo):
    from functools import reduce
    return reduce(lambda x,y:x and y,bo)

def Tuple(lis,n,head=list):
    p=itertools.product(lis,repeat=n)
    tuples=map(head,p)
    return tuples
    
def primelist(x):
    p = [2]
    n = 3
    while n < x:
        for i in p:
            if n % i == 0:
                break
        else:
            p.append(n)
        n += 2
    return p

def Norm(lis):
    return sum([i**2 for i in lis])**0.5
def Normalize(lis):
    norm=Norm(lis)
    return [i/norm for i in lis]
#判断类函数
def OddQ(num):
    return num%2==1
def EvenQ(num):
    return num%2==0
def PrimeQ(num):
    from sympy.mpmath.libmp import isprime
    return isprime(num)